package com.kanshu.ksgb.fastread.doudou.common.net;

import android.util.Log;

import retrofit2.Callback;


public abstract class SuperCallBack<T> implements Callback<ResponseData<T>> {


    @Override
    public void onResponse(retrofit2.Call<ResponseData<T>> call, retrofit2.Response<ResponseData<T>> response) {
        ResponseData<T> ResponseData = response.body();
        ResponseData.ResultBean resultData;
        if (ResponseData == null || ResponseData.getResult()==null) {
            Log.d(SuperCallBack.class.getSimpleName(), "onResponse - Status : " + response.code());
            this.onFailure(call, new Throwable("Unknown http parse error!"));
        } else {
            resultData = ResponseData.getResult();
            if(resultData.getStatus()==null){
                this.onFailure(call,new Throwable("Unknown response data format"));
                return;
            }
            if(resultData.getStatus().getCode()==0){
                this.onSuccess(call, (T)resultData.getData());
            }else{
                this.onFailure(call,new Throwable(resultData.getStatus().getMsg()));
            }
        }
        this.onFinish(call);
    }

    public abstract void onSuccess(retrofit2.Call<ResponseData<T>> call, T data);

    @Override
    public void onFailure(retrofit2.Call<ResponseData<T>> call, Throwable t) {
//        saveLog(call.request().url().toString(), t.getMessage());
        this.onFinish(call);
    }

    public void onFinish(retrofit2.Call<ResponseData<T>> call) {
    }
}
